package com.foo.designpattern.memento;

import junit.framework.TestCase;

/**
 * com.foo.designpattern.memento
 * Created with IntelliJ IDEA.
 * Created by jiang on 2016-05-03 12:26.
 *
 * @Description 测试类：备忘录模式
 * 本例比较简单，通过对原始信息简单的备份还原来解释备忘录模式，未做防修改原始信息的处理。
 * 防止备忘录里的状态被修改：http://www.cnblogs.com/java-my-life/archive/2012/06/06/2534942.html
 */

public class MementoTestCase extends TestCase {
    public void testMemento() {
        System.out.println("= = = = ↓↓↓备忘录模式，测试开始↓↓↓ = = = =");

        // 创建原始类
        Original original = new Original("apple");

        // 创建备忘录实例，并备份原始类的状态
        Storage storage = new Storage(original.createMemento());

        // 修改原始类的状态
        System.out.println("原始类的初始化状态为：" + original.getValue());
        original.setValue("banana");
        System.out.println("原始类的修改后状态为：" + original.getValue());

        // 通过使用备忘录里的信息恢复原始类的状态
        original.restoreMemento(storage.getMemento());
        System.out.println("原始类的恢复后状态为：" + original.getValue());

        System.out.println("= = = = ↑↑↑备忘录模式，测试结束↑↑↑ = = = =");
    }
}
